package cc.idoone.download.statistics;

import java.io.InputStream;
import java.util.Map;
import java.util.Properties;

/**
 * Web 请求相关工具
 * 
 * @author hemj
 * @date 2016年6月24日
 */
public class CommUtils {

	/** 设置分页参数 page,rows,rownum_offset,rownum_begin,rownum_end */
	public static Map addPaginationParams(Map params) {
		if (params.get("sum") == null)
			params.put("sum", "0");
		if (params.get("num") == null)
			params.put("num", "10");
		if ("-1".equals(params.get("num")))
			params.put("num", "30000");
		int iSum = Integer.parseInt((String) params.get("sum"));
		int iRows = Integer.parseInt((String) params.get("num"));
		int iPage = iSum / iRows + 1;
		int iRowEnd = iPage * iRows;
		int iRowOffset = iSum;
		int iRowBegin = iRowOffset + 1;
		params.put("page", iPage);
		params.put("rows", iRows);
		params.put("rownum_offset", iRowOffset);
		params.put("rownum_begin", iRowBegin);
		params.put("rownum_end", iRowEnd);
		return params;
	}

	/** 文档类型 */
	private static Properties mimeTypes = new Properties();
	static {
		InputStream in = null;
		try {
			in = CommUtils.class.getResourceAsStream("mime.properties");
			mimeTypes.load(in);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (in != null) {
				try {
					in.close();
					in = null;
				} catch (Exception e) {
				}
			}
		}
	}

	/** 获取下载文档的返回文档类型 */
	public static String getContentType(String extension) {
		return mimeTypes.getProperty(extension, "text/plain");
	}

}
